const app = getApp();
Page({
    data: {
        array: ["推荐", "喂养", "生长发育", "护理", "疾病"],
        background: ['/image/advertise01.png', '/image/advertise02.png', '/image/advertise03.png'],
        isChecked: 0,
        indicatorDots: false,
        vertical: false,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        sceneId: "hot",
        knowledgeContents: [],
        navHeight: app.globalData.statusBarHeight * 2 + 86,
        navbarData: {
            title: '知识库',
            showCapsule: true
        },
        pageNum: 1,
        pageCount: null,
        domain: app.globalData.config.educationServer
    },
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: app.globalData.tabIndex
            })
          
        }
    },
    onReachBottom() {
        if (this.data.pageNum < this.data.pageCount) {
            this.setData({
                pageNum: this.data.pageNum + 1
            })
            this.getEducationContent(this.data.pageNum);
        }
    },
    getEducationContent: function (pageNum) {
        var that = this;
        if ("age" == that.data.sceneId) {
            wx.request({
                url: app.getEducationUrl('/xcx/education/queryAgeEducationContent'),
                method: 'GET',
                header: wx.getStorageSync('cookie'),
                data: {
                    pageNum: that.data.pageNum,
                    userId: app.globalData.userId
                },
                success(res) {
                    var education = res.data.data.data;
                    that.data.knowledgeContents = [];
                    for (var i = 0; i < education.length; i++) {
                        var imgUrls = education[i].imgUrlList;
                        education[i].introduction = education[i].introduction.replace(/&nbsp;/ig, '');

                        for (var j = 0; j < imgUrls.length; j++) {
                            if (imgUrls[j].indexOf("http") == -1) {
                                imgUrls[j] = that.data.domain + imgUrls[j];
                            }
                        }
                        that.data.knowledgeContents.push(education[i]);
                    }
                    that.setData({
                        knowledgeContents: that.data.knowledgeContents,
                        pageNum: res.data.data.pageIndex,
                        pageCount: res.data.data.pageCount
                    })
                }
            })
        } else {
            wx.request({
                url: app.getEducationUrl('/xcx/education/queryEducationContent'),
                method: 'GET',
                header: wx.getStorageSync('cookie'),
                data: {
                    sceneId: that.data.sceneId,
                    pageNum: that.data.pageNum
                },
                success(res) {
                    var education = res.data.data.data;
                    that.data.knowledgeContents = [];
                    for (var i = 0; i < education.length; i++) {
                        var imgUrls = education[i].imgUrlList;
                        education[i].introduction = education[i].introduction.replace(/&nbsp;/ig, '');
                        for (var j = 0; j < imgUrls.length; j++) {
                            if (imgUrls[j].indexOf("http") == -1) {
                                imgUrls[j] = that.data.domain + imgUrls[j];
                            }
                        }
                        that.data.knowledgeContents.push(education[i]);
                    }
                    that.setData({
                        knowledgeContents: that.data.knowledgeContents,
                        pageNum: res.data.data.pageIndex,
                        pageCount: res.data.data.pageCount
                    })
                    console.log(that.data.knowledgeContents)
                }
            })
        }
    },
    onLoad: function () {
        var that = this;
        wx.request({
            url: app.getEducationUrl('/xcx/education/queryEducationScene'),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            success(res) {
                that.setData({
                    array: res.data.data
                })
                that.getEducationContent(that.data.pageNum);
            }
        })
    },
    // 改变点击
    changeCheck: function (e) {
        var _this = this;
        console.log(e)
        _this.setData({
            isChecked: e.currentTarget.dataset.id,
            sceneId: e.currentTarget.dataset.sceneId
        })
        if (getCurrentPages().length != 0) {
            //刷新当前页面的数据
            getCurrentPages()[getCurrentPages().length - 1].onLoad()
        }
    },
    toKnowledgeDetail: function (e) {
        wx.navigateTo({
            url: "../knowledge/knowledgeDetail/knowledgeDetail?id=" + e.currentTarget.dataset.id + "&channel=" + e.currentTarget.dataset.channel
        })
    }


})